package com.jiang.smartdangjian.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.jiang.smartdangjian.R;
import com.jiang.smartdangjian.model.NewsCateData;
import com.jiang.smartdangjian.model.NewsData;
import com.jiang.smartdangjian.tool.HttpUtil;
import com.jiang.smartdangjian.tool.MyAdapter;
import com.jiang.smartdangjian.tool.MyCallback;
import com.jiang.smartdangjian.tool.Tool;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Banner banner2;
    private CardView cv;
    private RecyclerView rvSer;
    private TabLayout tabLayout2;
    private RecyclerView rvNews;
    private NestedScrollView ns;
    List<Bean> beans = Arrays.asList(new Bean("党员学习", R.drawable.xx), new Bean("组织活动", R.drawable.hd),
            new Bean("建言献策", R.drawable.jy), new Bean("随手拍", R.drawable.ssp));
    List<NewsCateData.DataModel> cateList = new ArrayList<>();
    List<NewsData.RowsModel> newsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        initView();
    }

    private void initView() {
        banner2 = (Banner) findViewById(R.id.banner2);
        cv = (CardView) findViewById(R.id.cv);
        rvSer = (RecyclerView) findViewById(R.id.rv_ser);
        tabLayout2 = (TabLayout) findViewById(R.id.tabLayout2);
        rvNews = (RecyclerView) findViewById(R.id.rv_news);
        ns = (NestedScrollView) findViewById(R.id.ns);


        rvSer.setLayoutManager(new GridLayoutManager(this, 4));
        rvNews.setLayoutManager(new LinearLayoutManager(this));
        ns.setNestedScrollingEnabled(false);
        banner2.setImages(Arrays.asList(R.drawable.s1, R.drawable.s2, R.drawable.s3))
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object o, ImageView imageView) {
                        imageView.setImageResource((Integer) o);
                    }
                }).start();

        getData();
    }

    private void getData() {
        rvSer.setAdapter(new MyAdapter<Bean>(getThis(), R.layout.item_bean, beans) {
            private ImageView imageView2;
            private TextView textView2;

            @Override
            public void init(View root, int position) {
                imageView2 = (ImageView) root.findViewById(R.id.imageView2);
                textView2 = (TextView) root.findViewById(R.id.textView2);
                Bean bean = list.get(position);
                imageView2.setImageResource(bean.res);
                textView2.setText(bean.t);
                root.setOnClickListener(v -> {
                    if (position == 0) {
                        startActivity(new Intent(getThis(), S1Activity.class));
                    } else if (position == 1) {
                        startActivity(new Intent(getThis(), S2Activity.class));
                    } else if (position == 2) {
                        startActivity(new Intent(getThis(), S3Activity.class));
                    } else {
                        startActivity(new Intent(getThis(), S4Activity.class));
                    }
                });
            }

        });

        HttpUtil.get("/prod-api/press/category/list", new MyCallback(getThis(), NewsCateData.class) {
            @Override
            public void onFinish(Object obj) {
                NewsCateData data = (NewsCateData) obj;
                cateList.clear();
                cateList.addAll(data.data);
                tabLayout2.removeAllTabs();
                for (NewsCateData.DataModel datum : data.data) {
                    tabLayout2.addTab(tabLayout2.newTab().setText(datum.name));
                }

                tabLayout2.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        getNews(cateList.get(tab.getPosition()).id);
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
                if (!cateList.isEmpty()) {
                    getNews(cateList.get(0).id);
                }
            }
        });
    }

    private void getNews(int id) {
        HttpUtil.get("/prod-api/press/press/list?type=" + id, new MyCallback(getThis(), NewsData.class) {
            @Override
            public void onFinish(Object obj) {
                NewsData data = (NewsData) obj;

                rvNews.setAdapter(new MyAdapter<NewsData.RowsModel>(getThis(), R.layout.item_dj_news, data.rows) {
                    private ImageView imageView4;
                    private TextView textView4;
                    private TextView textView5;
                    private TextView textView7;

                    @Override
                    public void init(View root, int position) {
                        imageView4 = (ImageView) root.findViewById(R.id.imageView4);
                        textView4 = (TextView) root.findViewById(R.id.textView4);
                        textView5 = (TextView) root.findViewById(R.id.textView5);
                        textView7 = (TextView) root.findViewById(R.id.textView7);
                        NewsData.RowsModel rowsModel = list.get(position);
                        Tool.loadImg(rowsModel.cover, imageView4);
                        textView4.setText(rowsModel.title);
                        textView5.setText(rowsModel.content);
                        textView7.setText(rowsModel.createTime);
                        root.setOnClickListener(v -> {
                            Tool.news = rowsModel;
                            startActivity(new Intent(getThis(), NewsActivity.class));
                        });
                    }
                });
            }
        });
    }

    static class Bean {
        public Bean(String t, int res) {
            this.t = t;
            this.res = res;
        }

        String t;
        int res;
    }
}